package com.itsm.xkitsm.liferoot.life;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

import com.itsm.xkitsm.liferoot.MainAct;
import com.itsm.xkitsm.liferoot.R;
import com.itsm.xkitsm.liferoot.adapter.MyBaseAdapter_sp;
import com.itsm.xkitsm.liferoot.adapter.MyExpandAdapter;
import com.itsm.xkitsm.liferoot.life.weather.Execute_DB;
import com.itsm.xkitsm.liferoot.life.weather.Info_city;
import com.itsm.xkitsm.liferoot.life.weather.Info_province;
import com.itsm.xkitsm.liferoot.life.weather.Info_town;
import com.itsm.xkitsm.liferoot.life.weather.Instance;

public class AddActivity extends Activity implements OnClickListener {
	private ExpandableListView exlv_city;
	private ExpandableListAdapter adapter_exlv;
	private BaseAdapter adapter_sp;
	private ImageButton but_seach;
	private Spinner sp_province;
	public static List<Object> data_province;
	private String[] allName = new NameIDMap().getAllNames();
	public static String[] arr_town;
	public static String[][] arr_city;

	public static Execute_DB exe_db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.addcity);
		bind();
		exe_db = new Execute_DB(this);
		data_province = exe_db.query("%", Execute_DB.province_table);
		adapter_sp = new MyBaseAdapter_sp(this, data_province);
		sp_province.setAdapter(adapter_sp);

		sp_province.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				System.out.println("spinnerID:" + arg2);
				List<Object> data_town = exe_db.query(
						((Info_province) data_province.get(arg2)).getProvince(),
						Execute_DB.town_table);
				arr_town = new String[data_town.size()];
				arr_city = new String[data_town.size()][];
				for (int i = 0; i < data_town.size(); i++) {
					arr_town[i] = ((Info_town) data_town.get(i)).getTown();
					// System.out.println(arr_town[i]);
					List<Object> data_city = exe_db.query(arr_town[i],
							Execute_DB.city_table);
					String[] temp = new String[data_city.size()];
					for (int j = 0; j < data_city.size(); j++) {
						temp[j] = ((Info_city) data_city.get(j)).getCity();
						// System.out.println(temp[j]);
					}

					arr_city[i] = temp;
					MyExpandAdapter.updata(arr_city);

				}
				adapter_exlv = new MyExpandAdapter(data_town, AddActivity.this);
				exlv_city.setAdapter(adapter_exlv);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

		exlv_city.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView arg0, View arg1,
					int arg2, int arg3, long arg4) {
				// TODO Auto-generated method stub
				Toast.makeText(AddActivity.this, arr_city[arg2][arg3], 1000)
						.show();
				Instance.getInstance().setKey(arr_city[arg2][arg3]);
//				Intent it = new Intent(AddActivity.this,
//						MainAct.class);
//				startActivity(it);
				sendBroadcast(new Intent(MainAct.CHANGECITY));
				finish();
				return false;
			}
		});

	}

	private void bind() {
		sp_province = (Spinner) findViewById(R.id.sp_province);
		exlv_city = (ExpandableListView) findViewById(R.id.exlv_city);
		but_seach = (ImageButton) findViewById(R.id.but_seach);
		but_seach.setOnClickListener(this);
		ArrayAdapter<String> av = new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line, allName);
		AutoCompleteTextView auto = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
		auto.setAdapter(av);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.but_seach: {
		}
			break;
		}

	}

}
